<?php

/**
 * Формы прав/групп/пользователей.
 */
class AclForm extends Zend_Form {

    /**
     * Форма группы
     *
     * @param int $id #ID группы
     * @return Zend_Form
     */
    public function Group($id) {
        $this->setName('group');
        $t = Zend_Registry::get('Zend_Locale');
        $name = new Zend_Form_Element_Text('name');
        $name->setLabel('Group name')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty')
                ->setRequired();

        $description = new Zend_Form_Element_Textarea('description');
        $description->setLabel('Description')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');

        $model = new AclModel();
        $users = new Zend_Form_Element_MultiCheckbox('users');
        $array = $model->getUsersList();
        $users->addMultiOptions($array)
                ->setLabel('Users')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Save');
        $submit->setAttrib('id', 'submitbutton');

        $pageModel = new PageModel();
        $values = $pageModel->getAddonsList('page');
        $modules = new Zend_Form_Element_MultiCheckbox('modules');
        $modules->setLabel('Modules')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty')
                ->addMultiOptions($values);

        $values = $pageModel->getAddonsList('system');
        $admin = new Zend_Form_Element_MultiCheckbox('admin');
        $admin->setLabel('Administration')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty')
                ->addMultiOptions($values);

        $this->addElements(array($name, $description, $admin, $modules, $submit));
        $this->addElements(array($users));
        $this->addDisplayGroup(array('name', 'description'), 'settings', array('legend' => $t->_('Group')));
        $this->addDisplayGroup(array('admin', 'modules'), 'acls', array('legend' => $t->_('Permissions')));
        $this->addDisplayGroup(array('users', 'submit'), 'user', array('legend' => $t->_('Users')));

        $this->setDisplayGroupDecorators(array(
            'FormElements',
            'Fieldset'
        ));

        $this->setAction(ADMIN_URL . '/acl/group/id/' . $id);
        $this->setDefaultTranslator($t);
        return $this;
    }

    /**
     * Форма пользователя
     *
     * @param int $id #ID пользователя
     * @return Zend_Form
     */
    public function User($id, $onlyme = false) {
        $this->addElementPrefixPath('Model_User_Form_Validate', APP_PATH . "/forms/validate", 'validate');

        $t = Zend_Registry::get('Zend_Locale');
        $config = Zend_Registry::get('config');
        $this->setName('user');
        $name = new Zend_Form_Element_Text('name');
        $name->setLabel('Name')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty')
                ->setRequired();  

        $login = new Zend_Form_Element_Text('login');
        $login->setLabel('Login')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty')
                ->addValidator('Alnum');
        if ($id == 0) {
            $login->addValidator('Db_NoRecordExists', false, array(
                        array('table' => 'users',
                            'field' => 'login')
                    ))
                    ->setRequired();
        } else {
            $login->setAttrib('disabled', 'disabled');
        }

        $pass = new Zend_Form_Element_Password('password');
        $pass->setLabel('Password')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('StringLength', false, $config->pass_lenght)
                ->addValidator('NotEmpty');

        $pass2 = new Zend_Form_Element_Password('password2');
        $pass2->setLabel('Confirm password')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->setAllowEmpty(false)
                ->addValidator('PasswordConfirmation');

        $mail = new Zend_Form_Element_Text('email');
        $mail->setLabel('E-mail')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty')
                ->addValidator('EmailAddress')
                ->setRequired();

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Save');
        $submit->setAttrib('id', 'submitbutton');

        $active = new Zend_Form_Element_Checkbox('active');
        $active->setLabel('Active')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');
        
        if (!$onlyme) {
            $model = new AclModel();
            $array = $model->getGroupsList();
            $groups = new Zend_Form_Element_MultiCheckbox('groups');
            $groups->addMultiOptions($array)
                    ->setLabel('Groups')
                    ->addFilter('StripTags')
                    ->addFilter('StringTrim')
                    ->addValidator('NotEmpty');
            $this->addElements(array($name, $login, $pass, $pass2, $mail, $active, $groups, $submit));
        } else {
            $this->addElements(array($name, $login, $pass, $pass2, $mail, $active, $submit));
        }

        $this->setAction(ADMIN_URL . '/acl/user/id/' . $id);
        $this->setDefaultTranslator($t);
        return $this;
    }

    /**
     * Ajax-форма назначения прав
     *
     * @param string $object Имя объекта
     * @param int $id #ID объекта
     * @return Zend_Form
     */
    public function Acl($object, $id) {
        $t = Zend_Registry::get('Zend_Locale');
        $this->setName('ajax_form');
        $array_fields = array();
        $model = new AclModel();
        $groups = $model->getGroupsList();
        $acl_array = array(
            '0' => $t->_('No access'),
            '1' => $t->_('Read'),
            '2' => $t->_('Edit'),
            '3' => $t->_('Full access')
        );

        foreach ($groups as $key => $val) {
            $field = new Zend_Form_Element_Select('' . $key);
            $field->addMultiOptions($acl_array)
                    ->setLabel($val)
                    ->addFilter('StripTags')
                    ->addFilter('StringTrim')
                    ->addValidator('Int');
            $array_fields[] = $field;
        }
        $this->addElements($array_fields);
        $this->setAction(ADMIN_URL . '/acl/acl/object/' . $object . '/oid/' . $id);
        $this->setDefaultTranslator($t);
        return $this;
    }

    /**
     * Форма входа
     *
     * @return Zend_Form
     */
    public function Login($action = '') {
        $t = Zend_Registry::get('Zend_Locale');
        $config = Zend_Registry::get('config');
        $this->setName('loginform');

        $login = new Zend_Form_Element_Text('login');
        $login->setLabel('Login')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty')
                ->setRequired();

        $pass = new Zend_Form_Element_Password('password');
        $pass->setLabel('Password')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty')
                ->setRequired();

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Sign in');
        $submit->setAttrib('id', 'submitbutton');

        $this->addElements(array($login, $pass, $submit));

        $this->setAction($action);
        $this->setDefaultTranslator($t);
        return $this;
    }

    /**
     * Форма запроса пароля
     *
     * @return Zend_Form
     */
    public function Request($action = '') {
        $t = Zend_Registry::get('Zend_Locale');
        $config = Zend_Registry::get('config');
        $this->setName('loginform');

        $email = new Zend_Form_Element_Text('email');
        $email->setLabel('E-mail')
                ->addFilter('StringTrim')
                ->addValidator('EmailAddress')
                ->setRequired();
        //->addErrorMessages($t->_('E-mail not found'));

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Send');
        $submit->setAttrib('id', 'submitbutton');

        $this->addElements(array($email, $submit));

        $this->setAction($action);
        $this->setDefaultTranslator($t);
        return $this;
    }

    /**
     * Форма профайла пользователя
     *
     * @param int $id #ID пользователя
     * @return Zend_Form
     */
    public function Profile($id) {
        $this->addElementPrefixPath('Model_User_Form_Validate', APP_PATH . "/forms/validate", 'validate');

        $this->setDefaultTranslator(Zend_Registry::get('Zend_Locale'));
        $config = Zend_Registry::get('config');
        $this->setName('user');
        $name = new Zend_Form_Element_Text('name');
        $name->setLabel('Name')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty')
                ->setRequired();

        $login = new Zend_Form_Element_Text('login');
        $login->setLabel('Login');
        $login->setAttrib('disabled', 'disabled');

        $pass = new Zend_Form_Element_Password('password');
        $pass->setLabel('Password')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('StringLength', false, $config->pass_lenght)
                ->addValidator('NotEmpty');

        $pass2 = new Zend_Form_Element_Password('password2');
        $pass2->setLabel('Confirm password')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->setAllowEmpty(false)
                ->addValidator('PasswordConfirmation');

        $mail = new Zend_Form_Element_Text('email');
        $mail->setLabel('E-mail')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty')
                ->addValidator('EmailAddress')
                ->setRequired();

        $photo = new Zend_Form_Element_File('photo');
        $photo->setLabel('You photo')
                ->addValidator('NotEmpty');

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Save');
        $submit->setAttrib('id', 'submitbutton');

        $this->addElements(array($name, $login, $pass, $pass2, $mail, $photo, $submit));
        $this->setAction(SITE_URL . PAGE_URL . '/view/profile');
        return $this;
    }

}
